package com.cn.boot.spring.controller;
import com.cn.boot.spring.entity.RoleEntity;
import com.cn.boot.spring.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "角色管理")
@RequestMapping("/admin/role")
public class RoleController {
    @Autowired
    private IRoleService iRoleService;
    @ApiOperation("角色列表")
    @GetMapping("/findAll")
    public List<RoleEntity> findAll(){
        return iRoleService.findAll();
    }
    @ApiOperation("根据某一id查询角色1")
    @GetMapping("/findById1")
    public RoleEntity findById1(@RequestParam Integer id) {
        return iRoleService.findById(id);
    }
    @ApiOperation("根据某一id查询角色2")
    @GetMapping("/findById2/{id}")
    public RoleEntity findById2(@PathVariable Integer id) {
        return iRoleService.findById(id);
    }


    /**
     * 添加一条数据
     * Post请求要设置请求方法，参数注解要用@RequestBody
     */
    @ApiOperation("添加一个角色")
    @PostMapping("/addRole")
    public Integer addRole(@RequestBody RoleEntity roleEntity) {
        return iRoleService.addRole(roleEntity);
    }

    /**
     * 批量添加
     * 参数传入list
     */
    @ApiOperation("批量添加角色")
    @PostMapping("/addRoles")
    public Integer addRoles(@RequestBody List<RoleEntity> roles) {
        iRoleService.addRoles(roles);
//        System.out.println(users);
        //添加几条就返回几条
        return roles.size();
    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除角色")
    @DeleteMapping("/del")
    public Integer del(@RequestBody List<Integer> ids) {
        iRoleService.del(ids);
        //删除几条就返回几条
        return ids.size();
    }
    /**
     * 更新
     */
    @ApiOperation("更新数据")
    @PostMapping("/update")
    public Integer update(@RequestBody RoleEntity roleEntity){
        return iRoleService.update(roleEntity);
    }
    
}
